package com.cs410.collabwriting.junit.main;

import java.util.ArrayList;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import com.cs410.collabwriting.main.Comment;
import com.cs410.collabwriting.main.Story;
import com.cs410.collabwriting.main.StoryManager;
import com.cs410.collabwriting.main.User;
import com.cs410.collabwriting.main.UserManager;
import com.cs410.collabwriting.test.DatabaseUtility;
import com.cs410.collabwriting.tools.DatabaseManager;
import com.cs410.collabwriting.tools.LogManager;
import com.cs410.collabwriting.tools.queryTools.DatabaseTable;
import com.cs410.collabwriting.tools.queryTools.QueryBuilder;
import com.cs410.collabwriting.tools.queryTools.QueryCondition;
import com.cs410.collabwriting.tools.queryTools.QueryType;
import com.cs410.collabwriting.tools.queryTools.tables.COMMENT_TABLE;

import junit.framework.Assert;

public class CommentTest {
    @Rule
    public TestName m_testName = new TestName();
    private static User test_user;
    
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        LogManager.logInfo("[JUnitTest] -- Starting JUnit Tests for StoryManager" );
        DatabaseUtility.DeleteAllTables();        
        Thread.sleep(1000);        
        DatabaseUtility.CreateAllTables();        
        Thread.sleep(1000);        
        DatabaseUtility.CreateAllTestingData();
        Thread.sleep(15000);
        
        test_user = UserManager.getInstance().addUser("test_user", "password", "email");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        LogManager.logInfo("[JUnitTest] -- Finished JUnit Tests for StoryManager" );
    }

    @Before
    public void setUp() throws Exception {
        LogManager.logInfo("[JUnitTest] -- Starting Test : " + m_testName.getMethodName() );
    }

    @After
    public void tearDown() throws Exception {
        LogManager.logInfo("[JUnitTest] -- Finished Test : " + m_testName.getMethodName() );
    }
    
    @Test
    public void getterTests() {
        Story story = StoryManager.getInstance().getStoryByID( 2 );
        story.comment(test_user.getUserID(), m_testName.getMethodName());
        
        ArrayList<String[]> comments = DatabaseManager.executeQuery(
            new QueryBuilder(QueryType.SELECTION)
            .addColumns( COMMENT_TABLE.COMMENTID )
            .addTables( DatabaseTable.COMMENT_TABLE )
            .addConditions( QueryCondition.AND, COMMENT_TABLE.COMMENT, QueryCondition.EQ,  m_testName.getMethodName() ) 
            .addConditions( QueryCondition.AND, COMMENT_TABLE.STORYID, QueryCondition.EQ, story.getStoryID())
            .addConditions( QueryCondition.AND, COMMENT_TABLE.USERID, QueryCondition.EQ, test_user.getUserID())
            .value() );
        
        Comment c = new Comment(Integer.parseInt(comments.get(0)[0]));
        Assert.assertEquals(m_testName.getMethodName(), c.getComment());
        Assert.assertEquals(Integer.parseInt(comments.get(0)[0]), c.getCommentID());
        Assert.assertEquals(test_user.getUserID(), c.getUserID());
        Assert.assertEquals(story.getStoryID(), c.getStoryID());
        Assert.assertNotNull(c.getTimeStamp());
        Assert.assertNotNull(c.getTimeStampinNum());
    }
}
